const uiFormControls = require('./ui-form-controls');
let formTarget = {};
// index.js
Component({
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        disabled: {
            type: Boolean,
            value: false
        },
        validate: {
            type: Boolean,
            value: false
        },
        title: {
            type: String,
            value: ''
        },
        desc: {
            type: String,
            value: ''
        },
        labelWidth: {
            type: Number,
            value: 0
        },
    },
    relations: {
        'uiFormControls': {
            type: 'descendant', // 关联的目标节点应为子孙节点
            target: uiFormControls,
            linked: function(target) {
                const that = this;
                if (!target.data.name) {
                    throw new Error("ui-form must have name property");
                }
                if (target.data.name in formTarget) {
                    throw new Error("ui-form name must be unique");
                }
                formTarget[target.data.name] = target;
                target["dataChange"] = (res) => {
                    target.triggerEvent( target.data.eventName , res);
                    that.sonChange(res);
                };
                target.setData({
                    validate: this.data.validate,
                    disabled: this.data.disabled,
                    labelWidth: this.data.labelWidth,
                })
            }
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        sons: [],
    },
    lifetimes: {
        attached: function () {
            console.log(this.data);
        },
        detached() {
            formTarget = {};
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        sonChange: function (e) {
            const that = this;
            that.setData({
                formData: {
                    ...that.data.formData,
                    [e.name]: e.value
                }
            });
        },
    }
})